﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Toth.WLW.Plugins
{
	public partial class formHelloTxtAPIKey : Form
	{
		public formHelloTxtAPIKey()
		{
			InitializeComponent();
		}

        public formHelloTxtAPIKey(string helloTxtAPIKey)
			: this()
		{
            textBoxHelloTxtAPIKey.Text = helloTxtAPIKey;
		}

		private void formHelloTxtAPIKey_FormClosing(object sender, FormClosingEventArgs e)
		{
            if (backgroundWorkerValidateHelloTxtUser.IsBusy)
            {
                e.Cancel = true;
            }
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
            if (string.IsNullOrEmpty(textBoxHelloTxtAPIKey.Text))
            {
                DialogResult = DialogResult.OK;
                this.Close();
            }
            else if (backgroundWorkerValidateHelloTxtUser.IsBusy)
            {
                return;
            }
            else
            {

                this.Cursor = Cursors.WaitCursor;
                this.Enabled = false;

                backgroundWorkerValidateHelloTxtUser.RunWorkerAsync(textBoxHelloTxtAPIKey.Text.Trim());

            }

		}

        private void backgroundWorkerValidateHelloTxtUser_DoWork(object sender, DoWorkEventArgs e)
        {
            // Validate the API key
            HelloTxt.HelloTxtApi api = new HelloTxt.HelloTxtApi(e.Argument as string);

            HelloTxt.HelloTxtApi.ValidateResponse validateResponse = api.Validate();

            if (api.WebException != null)
            {
                throw api.WebException;
            }
            else if (api.DeserializeException != null)
            {
                throw api.DeserializeException;
            }
            else if (validateResponse == null)
            {
                throw new Exception("Could not connect to HelloTxt api services. Please check your internet connection.");
            }

            if (string.Equals(validateResponse.Status, "OK", StringComparison.InvariantCultureIgnoreCase))
            {
                e.Result = true;
            }
            else
            {
                e.Result = false;
            }
        }

        private void backgroundWorkerValidateHelloTxtUser_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // Re-enable UI
            this.Cursor = Cursors.Default;
            this.Enabled = true;

            if (e.Error != null)
            {
                MessageBox.Show(this, "Could not connect to HelloTxt api services. Please check your internet connection.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (e.Cancelled)
            {
                return;
            }

            if ((bool)e.Result)
            {
                DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                // Invalid, show an error
                MessageBox.Show(this, "Could not validate your API key. Please try another key.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void linkLabelFindApiKey_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://hellotxt.com/dashboard/applications");
        }



	}
}
